#include "dvStyledItemDelegate.h"
#include "dvDatabaseController.h"

dvStyledItemDelegate::dvStyledItemDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
    m_map = 0;
}

QWidget *dvStyledItemDelegate::createEditor(QWidget *parent,
                                            const QStyleOptionViewItem &option,
                                            const QModelIndex &index) const
{
    Q_UNUSED(option);

    if (index.column() == MAIN_TABLE_DATA)
    {
        QDateTimeEdit *editor = new QDateTimeEdit(parent);
        editor->setDisplayFormat(DV_DATE_FORMAT);
        editor->setCalendarPopup(true);
        return editor;
    }

    if (index.column() == MAIN_TABLE_REGION)
    {
        QComboBox *editor = new QComboBox(parent);
        if (m_dbCtrl)
        {
            QSqlQuery regions(m_dbCtrl->db());
            if (m_dbCtrl->getRegionsForMap(m_map,&regions))
            {
                while(regions.next())
                {
                    editor->addItem(regions.value(REGIONS_NAME).toString(),
                                    regions.value(REGIONS_ID).toInt());
                }
            }
        }
        return editor;
    }

    QLineEdit *editor = new QLineEdit(parent);
    editor->setValidator(new QDoubleValidator(editor));
    return editor;
}

void dvStyledItemDelegate::setEditorData(QWidget *editor,
                                         const QModelIndex &index) const
{
    QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
    if (edit)
    {
        edit->setText(index.model()->data(index, Qt::EditRole).toString());
    }
    else
    {
        QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
        if (dateEditor)
        {
            dateEditor->setDate(QDate::fromString(
            index.model()->data(index, Qt::EditRole).toString(),
            DV_DATE_FORMAT));
        }
        else
        {
            QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
            if (comboBox)
            {
                int item = comboBox->findData(index.model()->data(index, Qt::UserRole + 1));
                comboBox->setCurrentIndex(item);
            }
        }
    }
}

void dvStyledItemDelegate::setModelData(QWidget *editor,
                                        QAbstractItemModel *model,
                                        const QModelIndex &index) const
{
    QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
    if (edit)
    {
        if (!edit->text().isEmpty())
        {
            model->setData(index, QVariant(edit->text().toDouble()));
        }
        else
        {
            model->setData(index, QVariant(edit->text()));
        }
    }
    else
    {
        QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
        if (dateEditor)
        {
            model->blockSignals(true);
            QVariant oldData = model->data(index,Qt::EditRole);
            if (!oldData.isNull())
                model->setData(index, oldData, Qt::UserRole + 2);
            else
                model->setData(index, dateEditor->date().toString(DV_DATE_FORMAT), Qt::UserRole + 2);
            model->blockSignals(false);
            model->setData(index, dateEditor->date().toString(DV_DATE_FORMAT));
        }
        else
        {
            QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
            if (comboBox)
            {
                model->blockSignals(true);
                QVariant oldData = model->data(index,Qt::UserRole + 1);
                if (!oldData.isNull())
                    model->setData(index, oldData, Qt::UserRole + 2);
                else
                    model->setData(index,comboBox->itemData(comboBox->currentIndex()),Qt::UserRole + 2);
                model->setData(index,comboBox->itemData(comboBox->currentIndex()),Qt::UserRole + 1);
                model->blockSignals(false);
                model->setData(index,comboBox->itemText(comboBox->currentIndex()));
            }
        }
    }
}
